using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using SysOperationLayer.DBHandle;
using SysComm;
using IPMS.BLL;
using IPMS.Sort;
using Resources;


public partial class OptSearchProjectPrint : Page, IRequiresSessionState
{
    protected void CreateFolder(string path)
    {
        try
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
        finally
        {
        }
    }
    protected void btnExport_Click1(object sender, EventArgs e)
    {
        this.CreateFolder(base.Server.MapPath("~/Excel/"));
        if (this.ExportToExcel() == 0)
        {
            FileInfo info = new FileInfo(base.Server.MapPath("~/Excel/ExcelFileName.xls"));
            base.Response.Clear();
            base.Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFilename.xls");
            base.Response.AddHeader("Content-Length", info.Length.ToString());
            base.Response.ContentType = "application/vnd.xls";
            base.Response.WriteFile(info.FullName);
            base.Response.End();
        }
    }

    private int ExportToExcel()
    {

        StringWriter oStringWriter = null;
        HtmlTextWriter oHtmlTextWriter = null;
        StreamWriter objStreamWriter = null;
        try
        {
            string strFilePath = string.Empty;
            strFilePath = base.Server.MapPath(@"Excel\") + "ExcelFileName.xls";
            if (File.Exists(strFilePath))
            {
                File.Delete(strFilePath);
            }
            oStringWriter = new StringWriter();
            oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            objStreamWriter = File.AppendText(strFilePath);

            this.gvResults.RenderControl(oHtmlTextWriter);
            objStreamWriter.WriteLine(oStringWriter.ToString());
            objStreamWriter.Close();
            return 0;
        }
        catch (Exception ex)
        {
            lblError.Text = "ERROR:" + ex.Message;
            if (objStreamWriter != null)
            {
                objStreamWriter.Close();
            }
            return -1;
        }
    }


    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strUserID = Session[SysUtil.m_str_session_loginname].ToString();
            string strIP = HttpContext.Current.Request.UserHostAddress;
            ProjectDB objDB = new ProjectDB();

            gvResults.DataSource = objDB.GetInfo(base.Request.QueryString["ProjectGroup"].ToString(),
                                                base.Request.QueryString["Project"].ToString(),
                                                base.Request.QueryString["Type"].ToString(),
                                                base.Request.QueryString["TitleSubject"].ToString(),
                                                base.Request.QueryString["Country"].ToString(),
                                                base.Request.QueryString["Status"].ToString(),
                                                base.Request.QueryString["Applicant"].ToString(),
                                                base.Request.QueryString["Inventor"].ToString(),
                                                base.Request.QueryString["ResponsiblePerson"].ToString(),
                                                base.Request.QueryString["Agent"].ToString(),
                                                base.Request.QueryString["Client"].ToString(),
                                                base.Request.QueryString["ProjectClass"].ToString(),
                                                base.Request.QueryString["StartingNextDueDate"].ToString(),
                                                base.Request.QueryString["EndingNextDueDate"].ToString(),
                                                false,
                                                strUserID,
                                                strIP);

            gvResults.DataBind();
            objDB.CloseDB();
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}
